package com.example.androidproject.utils;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.example.androidproject.MyApp;
import com.example.androidproject.R;
import com.example.androidproject.data.Config;
import com.hyg.common.utils.CrashHandler;
import com.hyg.common.utils.DeviceIdUtil;
import com.hyg.common.utils.LogManager;
import com.hyg.common.utils.LogUtils;
import com.hyg.common.utils.language.LanguageUtil;

import java.io.File;

/**
 * FileName: LogUploadUtil
 * Author: huangyuguang
 * Date: 2022/9/30
 * Description: 日志管理
 */
public class LogUploadUtil {
    private static final String TAG = "LogUploadUtil";

    public static void uploadAllLogs(){
        uploadLogs(LogManager.getInstance().getLogDirectory());
        uploadLogs(CrashHandler.getInstance().getCrashLogPath());
    }

    public static void uploadTodayAllLogs(){
        uploadTodayLogs(LogManager.getInstance().getLogDirectory());
        uploadTodayLogs(CrashHandler.getInstance().getCrashLogPath());
    }

    private static void uploadLogs(String logPath){
        File dFile = new File(logPath);
        if(!dFile.exists()) return;
        File[] files = dFile.listFiles();
        String today = LogManager.getInstance().getTodayDate();
        if(files != null && files.length > 0){
            for(File file: files){
                if(file.isFile()){
                    uploadLog(file);
                }
            }
        }
    }

    private static void uploadTodayLogs(String logPath){
        File dFile = new File(logPath);
        if(!dFile.exists()) return;
        File[] files = dFile.listFiles();
        String today = LogManager.getInstance().getTodayDate();
        if(files != null && files.length > 0){
            for(File file: files){
                if(file.isFile()){
                    if(file.getName().contains(today)){
                        uploadLog(file);
                    }
                }
            }
        }
    }

    private static void uploadLog(File logFile){
        String dirName = "logs";
        String uploadFilePath = logFile.getAbsolutePath();
        if(uploadFilePath.contains("crashLogs")){
            dirName = "crashLogs";
        }
        String objectKey = String.format("%s/%s/%s/%s", LanguageUtil.getStringToEnglish(MyApp.getApp(),R.string.app_name), DeviceIdUtil.getDeviceId(MyApp.getApp()),dirName,logFile.getName());
        PutObjectRequest put = new PutObjectRequest(Config.OSS_BUCKET, objectKey, logFile.getAbsolutePath());

        put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
            @Override
            public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
                LogUtils.d("PutObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
            }
        });

        MyApp.oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
            @Override
            public void onSuccess(final PutObjectRequest request, PutObjectResult result) {
                String url = MyApp.oss.presignPublicObjectURL(Config.OSS_BUCKET, objectKey);
                LogUtils.d(TAG, String.format("log日志上传成功 -> %s",url));
            }

            @Override
            public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
                LogUtils.d(TAG, "log日志上传失败");
                // 请求异常
                if (clientExcepion != null) {
                    // 本地异常如网络异常等
                    clientExcepion.printStackTrace();
                }
                if (serviceException != null) {
                    // 服务异常
                    serviceException.printStackTrace();
                }
            }
        });
    }
}
